/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package test.controllers;

import java.io.Serializable;
import javax.persistence.Query;
import javax.persistence.EntityNotFoundException;
import javax.persistence.criteria.CriteriaQuery;
import javax.persistence.criteria.Root;
import test.entities.Client;
import test.entities.Action;
import java.util.ArrayList;
import java.util.Collection;
import java.util.List;
import javax.persistence.EntityManager;
import javax.persistence.EntityManagerFactory;
import test.controllers.exceptions.NonexistentEntityException;
import test.controllers.exceptions.PreexistingEntityException;
import test.entities.OrdreAchat;

/**
 *
 * @author Issam
 */
public class OrdreAchatJpaController implements Serializable {

    public OrdreAchatJpaController(EntityManagerFactory emf) {
        this.emf = emf;
    }
    private EntityManagerFactory emf = null;

    public EntityManager getEntityManager() {
        return emf.createEntityManager();
    }

    public void create(OrdreAchat ordreAchat) throws PreexistingEntityException, Exception {
        if (ordreAchat.getActionCollection() == null) {
            ordreAchat.setActionCollection(new ArrayList<Action>());
        }
        EntityManager em = null;
        try {
            em = getEntityManager();
            em.getTransaction().begin();
            Client email = ordreAchat.getEmail();
            if (email != null) {
                email = em.getReference(email.getClass(), email.getEmail());
                ordreAchat.setEmail(email);
            }
            Collection<Action> attachedActionCollection = new ArrayList<Action>();
            for (Action actionCollectionActionToAttach : ordreAchat.getActionCollection()) {
                actionCollectionActionToAttach = em.getReference(actionCollectionActionToAttach.getClass(), actionCollectionActionToAttach.getNumAction());
                attachedActionCollection.add(actionCollectionActionToAttach);
            }
            ordreAchat.setActionCollection(attachedActionCollection);
            em.persist(ordreAchat);
            if (email != null) {
                email.getOrdreAchatCollection().add(ordreAchat);
                email = em.merge(email);
            }
            for (Action actionCollectionAction : ordreAchat.getActionCollection()) {
                actionCollectionAction.getOrdreAchatCollection().add(ordreAchat);
                actionCollectionAction = em.merge(actionCollectionAction);
            }
            em.getTransaction().commit();
        } catch (Exception ex) {
            if (findOrdreAchat(ordreAchat.getNumAchat()) != null) {
                throw new PreexistingEntityException("OrdreAchat " + ordreAchat + " already exists.", ex);
            }
            throw ex;
        } finally {
            if (em != null) {
                em.close();
            }
        }
    }

    public void edit(OrdreAchat ordreAchat) throws NonexistentEntityException, Exception {
        EntityManager em = null;
        try {
            em = getEntityManager();
            em.getTransaction().begin();
            OrdreAchat persistentOrdreAchat = em.find(OrdreAchat.class, ordreAchat.getNumAchat());
            Client emailOld = persistentOrdreAchat.getEmail();
            Client emailNew = ordreAchat.getEmail();
            Collection<Action> actionCollectionOld = persistentOrdreAchat.getActionCollection();
            Collection<Action> actionCollectionNew = ordreAchat.getActionCollection();
            if (emailNew != null) {
                emailNew = em.getReference(emailNew.getClass(), emailNew.getEmail());
                ordreAchat.setEmail(emailNew);
            }
            Collection<Action> attachedActionCollectionNew = new ArrayList<Action>();
            for (Action actionCollectionNewActionToAttach : actionCollectionNew) {
                actionCollectionNewActionToAttach = em.getReference(actionCollectionNewActionToAttach.getClass(), actionCollectionNewActionToAttach.getNumAction());
                attachedActionCollectionNew.add(actionCollectionNewActionToAttach);
            }
            actionCollectionNew = attachedActionCollectionNew;
            ordreAchat.setActionCollection(actionCollectionNew);
            ordreAchat = em.merge(ordreAchat);
            if (emailOld != null && !emailOld.equals(emailNew)) {
                emailOld.getOrdreAchatCollection().remove(ordreAchat);
                emailOld = em.merge(emailOld);
            }
            if (emailNew != null && !emailNew.equals(emailOld)) {
                emailNew.getOrdreAchatCollection().add(ordreAchat);
                emailNew = em.merge(emailNew);
            }
            for (Action actionCollectionOldAction : actionCollectionOld) {
                if (!actionCollectionNew.contains(actionCollectionOldAction)) {
                    actionCollectionOldAction.getOrdreAchatCollection().remove(ordreAchat);
                    actionCollectionOldAction = em.merge(actionCollectionOldAction);
                }
            }
            for (Action actionCollectionNewAction : actionCollectionNew) {
                if (!actionCollectionOld.contains(actionCollectionNewAction)) {
                    actionCollectionNewAction.getOrdreAchatCollection().add(ordreAchat);
                    actionCollectionNewAction = em.merge(actionCollectionNewAction);
                }
            }
            em.getTransaction().commit();
        } catch (Exception ex) {
            String msg = ex.getLocalizedMessage();
            if (msg == null || msg.length() == 0) {
                Long id = ordreAchat.getNumAchat();
                if (findOrdreAchat(id) == null) {
                    throw new NonexistentEntityException("The ordreAchat with id " + id + " no longer exists.");
                }
            }
            throw ex;
        } finally {
            if (em != null) {
                em.close();
            }
        }
    }

    public void destroy(Long id) throws NonexistentEntityException {
        EntityManager em = null;
        try {
            em = getEntityManager();
            em.getTransaction().begin();
            OrdreAchat ordreAchat;
            try {
                ordreAchat = em.getReference(OrdreAchat.class, id);
                ordreAchat.getNumAchat();
            } catch (EntityNotFoundException enfe) {
                throw new NonexistentEntityException("The ordreAchat with id " + id + " no longer exists.", enfe);
            }
            Client email = ordreAchat.getEmail();
            if (email != null) {
                email.getOrdreAchatCollection().remove(ordreAchat);
                email = em.merge(email);
            }
            Collection<Action> actionCollection = ordreAchat.getActionCollection();
            for (Action actionCollectionAction : actionCollection) {
                actionCollectionAction.getOrdreAchatCollection().remove(ordreAchat);
                actionCollectionAction = em.merge(actionCollectionAction);
            }
            em.remove(ordreAchat);
            em.getTransaction().commit();
        } finally {
            if (em != null) {
                em.close();
            }
        }
    }

    public List<OrdreAchat> findOrdreAchatEntities() {
        return findOrdreAchatEntities(true, -1, -1);
    }

    public List<OrdreAchat> findOrdreAchatEntities(int maxResults, int firstResult) {
        return findOrdreAchatEntities(false, maxResults, firstResult);
    }

    private List<OrdreAchat> findOrdreAchatEntities(boolean all, int maxResults, int firstResult) {
        EntityManager em = getEntityManager();
        try {
            CriteriaQuery cq = em.getCriteriaBuilder().createQuery();
            cq.select(cq.from(OrdreAchat.class));
            Query q = em.createQuery(cq);
            if (!all) {
                q.setMaxResults(maxResults);
                q.setFirstResult(firstResult);
            }
            return q.getResultList();
        } finally {
            em.close();
        }
    }

    public OrdreAchat findOrdreAchat(Long id) {
        EntityManager em = getEntityManager();
        try {
            return em.find(OrdreAchat.class, id);
        } finally {
            em.close();
        }
    }

    public int getOrdreAchatCount() {
        EntityManager em = getEntityManager();
        try {
            CriteriaQuery cq = em.getCriteriaBuilder().createQuery();
            Root<OrdreAchat> rt = cq.from(OrdreAchat.class);
            cq.select(em.getCriteriaBuilder().count(rt));
            Query q = em.createQuery(cq);
            return ((Long) q.getSingleResult()).intValue();
        } finally {
            em.close();
        }
    }
    
}
